package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategySonComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategySonCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategySonCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategySonCommentServiceImpl implements IStrategySonCommentService {

    @Autowired
    private StrategySonCommentMapper strategySonCommentMapper;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;



    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategySonComment> list = strategySonCommentMapper.selectForList(qo);
        return new PageInfo(list);
    }


    @Override
    public StrategySonComment selectCommentById(Long id) {
        return strategySonCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategySonComment strategySonComment) {
        //添加一级子评论
        strategySonComment.setCreateTime(new Date());
        strategySonCommentMapper.insert(strategySonComment);
        //主评论id
        Long id = strategySonComment.getParent().getId();
        //增加评论数
        StrategyComment strategyComment = strategyCommentMapper.selectByPrimaryKey(id);
        Integer newViewCount = strategyComment.getViewCount()+1;
        strategyCommentMapper.updateViewCount(newViewCount,strategyComment.getId());
    }


}
